using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Diagnostics;

namespace MeteorShower
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameOverView : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        Texture2D gameOverScreen;
        SpriteFont font;
        int s_y = 480;//uhhh what are these!??!?
        int s_x = 840;

        public GameOverView(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            Debug.WriteLine("GAME OVER VIEW INITIALIZE");
            // TODO: Add your initialization code here
            base.Initialize();
        }

        protected override void LoadContent()
        {
            Debug.WriteLine("GAME OVER LOAD CONTENT");
            spriteBatch = new SpriteBatch(GraphicsDevice);
            
            gameOverScreen = Game.Content.Load<Texture2D>(Game1.getTheme().getGameOverText());
            font = Game.Content.Load<SpriteFont>("Segoe14");
        }

        public void reloadContent()
        {
            this.LoadContent();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (Game1.getGameState().Equals("gameoverview"))
            {
                //Handling back button being pressed
                if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                {
                    Game1.setGameState("mainmenuview");
                    Game1.reset();
                }
                base.Update(gameTime);
            }
        }

        public override void Draw(GameTime gameTime)
        {
            if(Game1.getGameState().Equals("gameoverview")){
                Debug.WriteLine("Score: " + Game1.getScore() + " High: " + Game1.getHighScore());
                if(Game1.getScore()>Game1.getHighScore()){
                    Game1.setHighScore(Game1.getScore());
                }
                //background
                GraphicsDevice.Clear(Color.Black);
                spriteBatch.Begin();
                spriteBatch.Draw(gameOverScreen, new Vector2(s_x / 2, s_y / 2), null, Color.White, 0f, new Vector2(gameOverScreen.Width / 2, gameOverScreen.Height / 2), .7f, SpriteEffects.None, 0f);
                spriteBatch.DrawString(font, Game1.getScore().ToString(), new Vector2(s_x / 2 + 70, s_y / 2 - 30), Color.White);
                spriteBatch.DrawString(font, Game1.getHighScore().ToString(), new Vector2(s_x / 2 + 70, s_y / 2 + 50), Color.White);
                spriteBatch.End();

                base.Draw(gameTime);
            }
        }
    }
}
